﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Phone.Tasks;

namespace deviantARTWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.ViewModel.IsDataLoaded)
            {
                App.ViewModel.LoadData();
            }
        }

        private void Newest_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            Newest_Progress.IsIndeterminate = !Newest_Progress.IsIndeterminate;
            this.Newest_load();

        }

        void Newest_load()
        {
            WebClient web1 = new WebClient();
            web1.DownloadStringCompleted += new DownloadStringCompletedEventHandler(web1_DownloadStringCompleted);
            web1.DownloadStringAsync(new Uri("http://backend.deviantart.com/rss.xml?q=special%3Anewest&type=deviation", UriKind.RelativeOrAbsolute));

        }

        void web1_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
               
            //throw new NotImplementedException();
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }
            try
            {
                XNamespace ns = "http://search.yahoo.com/mrss/"; 
                //xmlns:media="http://search.yahoo.com/mrss"
                //txtXML.Text = e.Result;
                XElement twtElement = XElement.Parse(e.Result);
                var items = from item in twtElement.Descendants("channel").Elements("item")
                            select new rss_prop
                            {
                                //img = item.Element("enclosure").Attribute(("url").ToString()).Value,
                                //detail  = item.Element("description").Value,
                                //Title = item.Element("title").Value,
                                //---------------------------------------------------

                                img = item.Element(ns + "thumbnail").Attribute(("url").ToString()).Value,
                                title = item.Element("title").Value,
                                detail = item.Element(ns + "description").Value.Replace("]]>", "]]]]><![CDATA[>"),
                                date = item.Element("pubDate").Value,
                                link = item.Element("link").Value,                             
                            };
            Newest_Progress.IsIndeterminate = false;      
            lstNewest.ItemsSource = items;
            }
            catch
            {
				Newest_Progress.IsIndeterminate = false;
                MessageBox.Show("Cannot access to server, please try again next time.");
                
            }
        }

        private void lstNewest_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            rss_prop Obj = (rss_prop)lstNewest.SelectedValue;
            goweb = Obj.link;
            this.go2web(goweb);
            //WebBrowserTask gotoweb = new WebBrowserTask();
            //gotoweb.URL = Obj.link.ToString();
            //gotoweb.Show();
        }

        

        private void Popular_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            Popular_Progress.IsIndeterminate = true;
            this.Popp_load();

        }
        void Popp_load()
        {

            WebClient web2 = new WebClient();
            web2.DownloadStringCompleted += new DownloadStringCompletedEventHandler(web2_DownloadStringCompleted);
            web2.DownloadStringAsync(new Uri("http://backend.deviantart.com/rss.xml?q=special%3Apopular&type=deviation", UriKind.RelativeOrAbsolute));
        }

        void web2_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            
            //throw new NotImplementedException();
            if (e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }
            try
            {
                XNamespace ns = "http://search.yahoo.com/mrss/";
                //xmlns:media="http://search.yahoo.com/mrss"
                //txtXML.Text = e.Result;
                XElement twtElement = XElement.Parse(e.Result);
                var items = from item in twtElement.Descendants("channel").Elements("item")
                            select new rss_prop
                            {
                                //img = item.Element("enclosure").Attribute(("url").ToString()).Value,
                                //detail  = item.Element("description").Value,
                                //Title = item.Element("title").Value,
                                //---------------------------------------------------

                                img = item.Element(ns + "thumbnail").Attribute(("url").ToString()).Value,
                                title = item.Element("title").Value,
                                detail = item.Element(ns + "description").Value.Replace("]]>", "]]]]><![CDATA[>"),
                                date = item.Element("pubDate").Value,
                                link = item.Element("link").Value,
                            };
                Popular_Progress.IsIndeterminate = false;
                lstPop.ItemsSource = items;
            }
            catch
            {
                Popular_Progress.IsIndeterminate = false;
                MessageBox.Show("Cannot access to server, please try again next time.");
                
                
            }
        }

        

        private void lstPop_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            rss_prop Obj = (rss_prop)lstPop.SelectedValue;
            goweb = Obj.link;
            this.go2web(goweb);
            //WebBrowserTask gotoweb = new WebBrowserTask();
            //gotoweb.URL = Obj.link.ToString();
            //gotoweb.Show();
        }

        private void Newest_refresh_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			lstNewest.ItemsSource = "";
            Newest_Progress.IsIndeterminate = true;
			this.Newest_load();
        }

        private void Popp_refresh_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            lstPop.ItemsSource = "";
            Popular_Progress.IsIndeterminate = true;
            this.Popp_load();
            
			
        }

        public string goweb = "";

        private void DigiArt_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/digitalart/";
            this.go2web(goweb);
           
        }

        private void DVDesign_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/designbattle/";
            this.go2web(goweb);
        }

        private void Photo_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/photography/";
            this.go2web(goweb);
        }

        private void Tradi_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            goweb = "http://browse.deviantart.com/traditional/";
            this.go2web(goweb);
        }

        private void Artisan_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/artisan/";
            this.go2web(goweb);
        }

        private void Contest_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/contests/";
            this.go2web(goweb);
        }

        private void GamDev_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/gameart/";
            this.go2web(goweb);
        }

        private void DesignUI_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/designs/";
            this.go2web(goweb);
        }

        private void Cartoon_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/cartoons/";
            this.go2web(goweb);
        }

        private void Custom_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/customization/";
            this.go2web(goweb);
        }

        private void FracArt_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/fractals/";
            this.go2web(goweb);
        }

        private void FilmAni_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/film/";
            this.go2web(goweb);
        }

        private void Litera_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            WebBrowserTask gotoweb = new WebBrowserTask();
            gotoweb.URL = "http://browse.deviantart.com/literature/";
            gotoweb.Show();
        }

        private void Manga_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/manga/";
            this.go2web(goweb);
            
        }

        private void Anthro_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/anthro/";
            this.go2web(goweb);
        }

        private void ReStock_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/resources/";
            this.go2web(goweb);
        }

        private void Flash_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/flash/";
            this.go2web(goweb);

        }

        private void FanArt_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/fanart/";
            this.go2web(goweb);
        }

        private void CommProj_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            goweb = "http://browse.deviantart.com/projects/";
            this.go2web(goweb);
        }

        private void DVRelate_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            goweb = "http://browse.deviantart.com/darelated/";
            this.go2web(goweb);
        }

        private void Scraps_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            goweb = "http://browse.deviantart.com/scraps/";
            this.go2web(goweb);
        }

        void go2web(string goweb)
        {
            WebBrowserTask gotoweb = new WebBrowserTask();
            gotoweb.URL = goweb;
            gotoweb.Show();
        }


    }
}